home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / expand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-01  |  839 b   |  51 lines

  1. /*
  2.  * Do filename expansion with the shell.
  3.  */
  4.  
  5. #define EXPAND_BUF    2048
  6.  
  7. #include "sysincludes.h"
  8.  
  9. char *expand(char *input, char *ans)
  10. {
  11.     FILE *fp;
  12.     int last;
  13.     char buf[256];
  14.  
  15.     if (input == NULL)
  16.         return(NULL);
  17.     if (*input == '\0')
  18.         return("");
  19.                     /* any thing to expand? */
  20.     if (!strpbrk(input, "$*{}[]\\?~")) {
  21.         strcpy(ans, input);
  22.         return(ans);
  23.     }
  24.                     /* popen an echo */
  25.     sprintf(buf, "echo %s", input);
  26.  
  27.     fp = popen(buf, "r");
  28.     fgets(ans, EXPAND_BUF, fp);
  29.     pclose(fp);
  30.  
  31.     if (!strlen(ans)) {
  32.         strcpy(ans, input);
  33.         return(ans);
  34.     }
  35.  
  36.     /*
  37.      * A horrible kludge...  if the last character is not a line feed,
  38.      * then the csh has returned an error message.  Otherwise zap the
  39.      * line feed.
  40.      */
  41.     last = strlen(ans) - 1;
  42.     if (ans[last] != '\n') {
  43.         strcpy(ans, input);
  44.         return(ans);
  45.     }
  46.     else
  47.         ans[last] = '\0';
  48.  
  49.     return(ans);
  50. }
  51.